package com.gljx.web.controller.JournalController;
import com.gljx.web.Entity.JournalEntry.ExaminationRoomIncome;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.User;
import com.gljx.web.service.DrivingService;
import com.gljx.web.service.OwnCarTrainService;


/**
 * 
 * @author Administrator
 * 考场收入统计报表
 */
@Controller
@RequestMapping("/")
public class ExaminationRoomIncomeController {

	//自动注入
	@Autowired
	private OwnCarTrainService ownCarTrainService;
	
	//自动注入
	@Autowired
	private DrivingService drivingService;
	
	/**
	 * 查询当前月的考场收入统计报表
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryExaminationRoomIncome/look.action")
	public String ExaminationRoomIncome(HttpServletRequest request){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String startTime = sdf.format(new Date());
		request.setAttribute("beginTime", startTime);
		request.setAttribute("endTime", startTime);
		return "/backHome/journaling/financialJournaling/examinationRoomIncome/main";
	}
	
	/**
	 * 算出时间差
	 * @param request
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping("/queryExaminationRoomIncomeByTime")
	@ResponseBody
	public  List<String> queryExaminationRoomIncomeByTime(HttpServletRequest request,String startTime,String endTime){
	    List<String> time = TimeFormat.getMonthBetween(startTime, endTime);
	    return time;
	}
	
	@RequestMapping("/queryExaminationRoomIncomeData")
	@ResponseBody
	public Map<String, Object> queryExaminationRoomIncomeData(@RequestParam(value = "time[]") String[] time,HttpServletRequest request){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //定义返回类型
	    Map<String, Object> map = new HashMap<>();
	    //协议驾校
	    List<String> drivingName = new ArrayList<>();
	    //放空的驾校的值
	    List<ExaminationRoomIncome> examinationRoomIncomes = new ArrayList<>();
	    //根据时间循环次数
		for (int i = 0; i < time.length; i++) {
			//获取结束时间
			String end = TimeFormat.sumOneMonth(time[i]);
			//查询考前模拟收费
	    	List<ExaminationRoomIncome>  kqmns = ownCarTrainService.queryExaminationRoomIncome(time[i], end, user.getFcompanyid(), "2",null);
	    	//循环收集所有的协议驾校
	    	if(kqmns == null || kqmns.size() == 0){
	    		
	    	}else{
	    		for (ExaminationRoomIncome examinationRoomIncome : kqmns) {
	    			if(examinationRoomIncome == null){
	    				
	    			}else if(examinationRoomIncome.getDrivingName() == null || "".equals(examinationRoomIncome.getDrivingName())){
						examinationRoomIncomes.add(examinationRoomIncome);
						map.put("kong", "1");
					}else{
						drivingName.add(examinationRoomIncome.getDrivingName());
					}
				}
	    	}
	    	//未空的驾校也要加数据
	    	if(examinationRoomIncomes.size() <= i){
	    		examinationRoomIncomes.add(new ExaminationRoomIncome(null,null,null));
	    	}
		}
		//判断为空的驾校是否未空
		if(examinationRoomIncomes == null || examinationRoomIncomes.size() == 0){
			map.put("kqmn", "");
		}else{
			//放入未空的加下
			map.put("kqmnnull", examinationRoomIncomes);
		}
		//放入协议驾校为空的值
		map.put("kqmn", examinationRoomIncomes);
		if(drivingName == null || drivingName.size() == 0){
			
		}else{
			//根据条件查询驾校名称
			List<String> drivingName2 = drivingService.queryDrivingByInName(drivingName);
			//把所有的协议驾校传到页面
			map.put("drivingName", drivingName2);
			//循环月数
			for (int i = 0; i < time.length; i++) {
				//获取结束时间
				String end = TimeFormat.sumOneMonth(time[i]);
				List<ExaminationRoomIncome>  kqmns2 = new ArrayList<>();
				for (String string : drivingName2) {
					//查询考前模拟收费
					List<ExaminationRoomIncome>  kqmns = ownCarTrainService.queryExaminationRoomIncome(time[i], end, user.getFcompanyid(), "2",string);
					if(kqmns == null || kqmns.size() == 0){
						kqmns2.add(new ExaminationRoomIncome("",null,null));
					}else{
						kqmns2.add(kqmns.get(0));
					}
				}
				//放入协议驾校不为空的值
				map.put("kqmns"+i, kqmns2);
			}
		}
		return map;
	}
	
	@RequestMapping("/queryExaminationRoomIncomeData2")
	@ResponseBody
	public Map<String, Object> queryExaminationRoomIncomeData2(@RequestParam(value = "time[]") String[] time,HttpServletRequest request){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //定义返回类型
	    Map<String, Object> map = new HashMap<>();
	    //协议驾校
	    List<String> drivingName = new ArrayList<>();
	    //放空的驾校的值
	    List<ExaminationRoomIncome> examinationRoomIncomes = new ArrayList<>();
	    //根据时间循环次数
		for (int i = 0; i < time.length; i++) {
			//获取结束时间
			String end = TimeFormat.sumOneMonth(time[i]);
			//查询考前模拟收费
	    	List<ExaminationRoomIncome>  kqmns = ownCarTrainService.queryExaminationRoomIncome(time[i], end, user.getFcompanyid(), "1",null);
	    	//循环收集所有的协议驾校
	    	if(kqmns == null || kqmns.size() == 0){
	    		
	    	}else{
	    		for (ExaminationRoomIncome examinationRoomIncome : kqmns) {
	    			if(examinationRoomIncome == null){
	    			
	    			}else if(examinationRoomIncome.getDrivingName() == null || "".equals(examinationRoomIncome.getDrivingName())){
						examinationRoomIncomes.add(examinationRoomIncome);
						map.put("kong2", "1");
					}else{
						drivingName.add(examinationRoomIncome.getDrivingName());
					}
				}
	    	}
	    	//未空的驾校也要加数据
	    	if(examinationRoomIncomes.size() <= i){
	    		examinationRoomIncomes.add(new ExaminationRoomIncome(null,null,null));
	    	}
		}
		//判断为空的驾校是否未空
		if(examinationRoomIncomes == null || examinationRoomIncomes.size() == 0){
			map.put("kqmn2", "");
		}else{
			//放入未空的加下
			map.put("kqmnnull2", examinationRoomIncomes);
		}
		//放入协议驾校为空的值
		map.put("kqmn2", examinationRoomIncomes);
		if(drivingName == null || drivingName.size() == 0){
			
		}else{
			//根据条件查询驾校名称
			List<String> drivingName2 = drivingService.queryDrivingByInName(drivingName);
			//把所有的协议驾校传到页面
			map.put("drivingName2", drivingName2);
			//循环月数
			for (int i = 0; i < time.length; i++) {
				//获取结束时间
				String end = TimeFormat.sumOneMonth(time[i]);
				List<ExaminationRoomIncome>  kqmns2 = new ArrayList<>();
				for (String string : drivingName2) {
					//查询考前模拟收费
					List<ExaminationRoomIncome>  kqmns = ownCarTrainService.queryExaminationRoomIncome(time[i], end, user.getFcompanyid(), "1",string);
					if(kqmns == null || kqmns.size() == 0){
						kqmns2.add(new ExaminationRoomIncome("",null,null));
					}else{
						kqmns2.add(kqmns.get(0));
					}
				}
				//放入协议驾校不为空的值
				map.put("kqmns2"+i, kqmns2);
			}
		}
		return map;
	}
	
}
